import {EventBus} from "./lib/EventBus"

enum Commands{
    A,
    B,
    C
}

const bus = new EventBus<Commands>()

class Person {
    public constructor (public name: string, public age: number){

    }
}

let counter0 = 0
let counter1 = 0
let counter2 = 0

let id0 = bus.subscribe<Person>(Commands.A, (bus, data) => {
    counter0 ++
})

let id1 = bus.subscribe<Person>(Commands.A, (bus, data) => {
    counter0 ++
})

let id2 = bus.subscribe<Person>(Commands.B, (bus, data) => {
    counter1 ++
})

let id3 = bus.subscribe<Person>(Commands.C, (bus, data) => {
    counter2 ++
})

bus.publish(Commands.A, new Person("yeganaaa", 25))

let valid = true
valid = valid && counter0 == 2
valid = valid && counter1 == 0
valid = valid && counter2 == 0

bus.unSubscribe(id0)
bus.publish(Commands.A, new Person("yeganaaa", 25))

valid = valid && counter0 == 3
valid = valid && counter1 == 0
valid = valid && counter2 == 0

bus.publish(Commands.B, new Person("yeganaaa", 26))

valid = valid && counter0 == 3
valid = valid && counter1 == 1
valid = valid && counter2 == 0

bus.publish(Commands.C, new Person("yeganaaa", 26))
bus.publish(Commands.C, new Person("yeganaaa", 26))

valid = valid && counter0 == 3
valid = valid && counter1 == 1
valid = valid && counter2 == 2

bus.unSubscribeAll([id0, id1, id2, id3])

bus.publish(Commands.A, new Person("yeganaaa", 26))
bus.publish(Commands.B, new Person("yeganaaa", 26))
bus.publish(Commands.C, new Person("yeganaaa", 26))

valid = valid && counter0 == 3
valid = valid && counter1 == 1
valid = valid && counter2 == 2

if (!valid) throw "Unit test failure!"

console.log("Okay")